﻿using System;
using System.Linq;
using Autofac;
using ComputerVision.BaseForm;
using ComputerVision.Common;
using ComputerVision.Data;
using ComputerVision.PhieuDieuTraCoSoForm;
using ComputerVision.Services;
using ComputerVision.Services.User;
using QuanLyThongTinCoSo.Common;

namespace ComputerVision.CanBoQuanLyForm
{
    public partial class frmCanBoQuanLyDetail : frmDetail
    {
        private readonly IService<Phong> _phong;
        private readonly IService<Doi> _doiService;
        private frmPhieuDieuTraCoSoDetail _frmPhieuDieuTraCs;
        private readonly IService<CanBo> _canBoService;
        private readonly IUserService<aspnet_Users> _userService;

        public frmCanBoQuanLyDetail()
        {
            InitializeComponent();
            _phong = Program.Container.Resolve<IService<Phong>>();
            _doiService = Program.Container.Resolve<IService<Doi>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            _canBoService = Program.Container.Resolve<IService<CanBo>>();
            Text = @"Thêm mới Cán bộ";
            LoadPhong();
            drlPhong.Focus();
        }

        public frmCanBoQuanLyDetail(frmPhieuDieuTraCoSoDetail frm)
        {
            InitializeComponent();
            this._frmPhieuDieuTraCs = frm;
            _phong = Program.Container.Resolve<IService<Phong>>();
            _doiService = Program.Container.Resolve<IService<Doi>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            _canBoService = Program.Container.Resolve<IService<CanBo>>();
            Text = @"Thêm mới Cán bộ";
            LoadPhong();
            drlPhong.Focus();
        }

        private void LoadPhong()
        {
            drlPhong.DataSource = _phong.ToList().ToList();
            drlPhong.DisplayMember = "TenPhong";
            drlPhong.ValueMember = "Id";
        }

        private void LoadDoi(Guid idPhong)
        {
            drlDoi.DataSource = _doiService.ToList().Where(a => a.Phong_Id == idPhong).ToList();
            drlDoi.DisplayMember = "TenDoi";
            drlDoi.ValueMember = "Id";
        }

        public frmCanBoQuanLyDetail(Guid id)
        {
            InitializeComponent();
            _phong = Program.Container.Resolve<IService<Phong>>();
            _doiService = Program.Container.Resolve<IService<Doi>>();
            Program.Container.Resolve<IUserService<aspnet_Users>>();
            _canBoService = Program.Container.Resolve<IService<CanBo>>();
            Text = @"Sửa thông tin Cán bộ";
            LoadPhong();
            drlPhong.Focus();

            _fromtype = Constants.TypeForm.Edit;
            guid = id;

            var item = _canBoService.Detail(id);
            if (item != null)
            {
                drlPhong.SelectedValue =  item.Doi.Phong_Id;
                var phong = drlPhong.SelectedItem as Phong;
                var doi = _doiService.Detail(item.Doi_Id);
                if (phong != null)
                {
                    LoadDoi(phong.Id);
                }

                txtHoTen.Text = item.HoTen;
                txtDienThoai.Text = item.DienThoai;
                txtDienGiai.Text = item.DienGiai;
                drlDoi.SelectedValue = doi.Id;
            }
        }

        #region Validation

        protected override bool Validator()
        {
            bool flag = true;

            errorProvider.Dispose();
            if (drlDoi.SelectedItem == null)
            {
                errorProvider.SetError(drlDoi, "Bạn chưa nhập Đội. Xin vui lòng kiểm tra lại");
                flag = false;
            }
            if (string.IsNullOrEmpty(txtHoTen.Text))
            {
                errorProvider.SetError(txtHoTen, "Bạn chưa nhập họ tên. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            if (string.IsNullOrEmpty(txtDienThoai.Text))
            {
                errorProvider.SetError(txtDienThoai, "Bạn chưa nhập số điện thoại. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            return flag;
        }

        #endregion

        #region Insert
        protected override void Insert()
        {
            try
            {
                var doi = drlDoi.SelectedItem as Doi;
                var model = new CanBo
                {
                    Id = Guid.NewGuid(),
                    HoTen = txtHoTen.Text,
                    DienThoai = txtDienThoai.Text,
                    DienGiai = txtDienGiai.Text,
                    Doi_Id = doi.Id,
                    Created = DateTime.Now,
                    CreatedBy_Id = Constants.CurrentUser,
                    Modified = DateTime.Now,
                    ModifiedBy_Id = Constants.CurrentUser
                };
                _canBoService.Create(model);

                if (this._frmPhieuDieuTraCs != null)
                {
                    this._frmPhieuDieuTraCs.LoadCanBo();
                }
            }
            catch (Exception ex)
            {
                AlertList.Error("Lỗi phát sinh khi tạo mới dữ liệu.");
            }
        }
        #endregion

        #region Edit

        protected override void Edit()
        {
            var doi = drlDoi.SelectedItem as Doi;
            var item = _canBoService.Detail(guid);
            if (item != null)
            {
                item.HoTen = txtHoTen.Text;
                item.DienThoai = txtDienThoai.Text;
                item.DienGiai = txtDienGiai.Text;
                item.Doi_Id = doi.Id;
                item.Modified = DateTime.Now;
                item.ModifiedBy_Id = Constants.CurrentUser;
                _canBoService.Update(item);
            }
        }

        #endregion

        private void drlPhong_SelectedIndexChanged(object sender, EventArgs e)
        {
            var item = drlPhong.SelectedItem as Phong;
            if (item != null)
            {
                LoadDoi(item.Id);
            }
        }

    }
}
